package com.example.home_model.details;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.common.filed.DataActionEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.example.home_model.details.deleteEntity.DeleteEntity;
import com.example.home_model.details.entity.commentaddentity.DetailsEntity;
import com.example.home_model.details.entity.commententity.CommentEntity;
import com.example.network_skd.BaseEntity;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DetailsViewModel extends BaseViewModel<DetailsRepository> {

    private Long itemId;

    @Override
    protected void initData() {

    }

    @Override
    protected void onResult(BaseEntity baseEntity) {
        if (baseEntity instanceof CommentEntity){
            CommentEntity commentEntity = (CommentEntity) baseEntity;
            DataActionEntity dataActionEntity = new DataActionEntity();
            dataActionEntity.action="DATA";
            Map<String,Object> map = new HashMap<>();
            map.put("target","COMMENTLIST");
            map.put("COMMENTLIST",commentEntity.data.data);
            dataActionEntity.data =map;
            vmData.post(dataActionEntity);
        }else if (baseEntity instanceof DetailsEntity){
            ToastUtils.showLong("评论成功");
          requestCommentList(itemId);
        }else if (baseEntity instanceof DeleteEntity){
            requestCommentList(itemId);
        }
    }
    public void requestAddComment(Long itemId,String text){
        repository.requestAddComment(text,1,"1", itemId, SPUtils.getInstance().getLong("userId"),"",1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void requestCommentList(Long itemId){
        this.itemId = itemId;
        repository.requestCommentList(0l, itemId, 10, SPUtils.getInstance().getLong("userId"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void requestDelete(long commentId,long itemId){
        repository.requestDelete(commentId, itemId, SPUtils.getInstance().getLong("userId"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
}
